package org.sjm.samples.web;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.sjm.samples.model.CommonMessageResultVO;
import org.sjm.samples.model.ExamUserVO;
import org.sjm.samples.service.IUserService;
import org.sjm.samples.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

	@Autowired
	private IUserService userService;
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String toLogin(Model model){
		return "login/login";
	}
	
	@ResponseBody
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public CommonMessageResultVO doLogin(ExamUserVO vo, HttpSession session) throws Exception {
		CommonMessageResultVO messageResultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String, String>();
		messageResultVO.setSuccess(false);
		if(vo != null){
			ExamUserVO resultVO = null;
			try {
				resultVO = userService.getUserById(vo.getUserId());
				if(resultVO != null){
					if(vo.getPassword() != null){
						String encodePassword = MD5Utils.GetMd5Code(vo.getPassword());
						
						Date currentTime = null;
						Calendar currentTimecalendar = Calendar.getInstance();
						currentTimecalendar.setTime(new Date());
						currentTimecalendar.add(Calendar.MINUTE, -5);
						currentTime = currentTimecalendar.getTime();
						System.out.println(currentTime + "------------" + new Date());
						if(resultVO.getLastLockTime().before(currentTime)){
							System.out.println("不在锁定时间范围内");
							if(encodePassword.equals(resultVO.getPassword())){
								
								resultVO.setLoginFailCount(0);
								userService.editUser(resultVO);
								session.setAttribute("examUser", resultVO);
								messageResultVO.setSuccess(true);
							}else{
								resultVO.setLoginFailCount(resultVO.getLoginFailCount() + 1);
								if(resultVO.getLoginFailCount() == 3){
									resultVO.setLoginFailCount(0);
									resultVO.setLastLockTime(new Date());
									userService.editUser(resultVO);
								}
								userService.editUser(resultVO);
								messageMap.put("message", "用户名或密码错误");
							}
						}else{
							System.out.println("目前账号被锁定，无法登陆，请稍等5分钟。");
							messageMap.put("message", "目前账号被锁定，无法登陆，请稍等5分钟。");
						}
					}else{
						messageMap.put("message", "用户名或密码错误");
					}
				}else{
					messageMap.put("message", "用户名或密码错误");
				}
			} catch (Exception e) {
				messageMap.put("message", "用户名或密码错误");
				e.printStackTrace();
			}
		}
		messageResultVO.setMessageMap(messageMap);
		return messageResultVO;
	}
	
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		session.removeAttribute("examUser");
		return "redirect:/login";
	}
}
